<template>
  <el-dialog title="新增员工" :visible="showDialog" width="40%" @close="close">
    <!-- 表单 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-cascader
          v-model="form.departmentName"
          placeholder="请选择部门"
          :options="deptList"
          :props="{ label: 'name', value: 'name', expandTrigger: 'hover' }"
          @focus="getDeptList"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择聘用形式">
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template #footer>
      <el-button size="small" @click="close">取消</el-button>
      <el-button type="primary" size="small" @click="addEmployee">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import employeeEnum from '@/api/constant/employees'
import { getDepartmentList } from '@/api/departments'
import { listToTree } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据, 字段名根据接口文档约定
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      hireType: employeeEnum.hireType, // 聘用形式
      deptList: [], // 部门列表
      // 表单校验规则 默认触发方式 = trigger + blur
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空' },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹框
    close() {
      this.$emit('update:showDialog', false)
      // 重置表单和校验
      this.$refs.form.resetFields()
      // 数组重置后有一个空数组
      this.form.departmentName = ''
    },
    // 级联选择框获取焦点时
    async getDeptList() {
      const { depts } = await getDepartmentList()
      this.deptList = listToTree(depts)
    },
    // 新增员工
    async addEmployee() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      await addEmployee({
        ...this.form,
        departmentName: this.form.departmentName.slice(-1)[0]
      })
      this.$message.success('添加成功')
      this.$emit('update:showDialog', false)
      // 通知父组件重新渲染
      this.$emit('updateList')
    }
  }
}
</script>

<style>

</style>
